package logic

import (
	"context"

	"friend/internal/svc"
	"friend/pb/friend"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetRelationListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetRelationListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRelationListLogic {
	return &GetRelationListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetRelationList 好友列表
func (l *GetRelationListLogic) GetRelationList(in *friend.RelationListRequest) (*friend.RelationListResponse, error) {
	total, err := l.svcCtx.FriendRelationExtendModel.Count(l.ctx, in.UserId)
	if err != nil {
		return nil, err
	}
	list, err := l.svcCtx.FriendRelationExtendModel.FindList(l.ctx, in.UserId, in.Offset, in.Limit)
	if err != nil {
		return nil, err
	}

	var data []*friend.RelationInfo
	for _, item := range list {
		data = append(data, &friend.RelationInfo{
			Id:       item.Id,
			UserId:   item.UserId,
			FriendId: item.FriendId,
			Status:   item.Status,
			CreateAt: item.CreatedAt.Unix(),
			UpdateAt: item.UpdatedAt.Unix(),
		})
	}
	return &friend.RelationListResponse{Total: total, Data: data}, nil
}
